/********************************************************/
/*	Copyright (C) 2012 Gong Li Bin			*/
/*	Project:	MapReduce			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_05_20			*/
/*	File:		MrAdminTest.cpp			*/
/********************************************************/

#include "MrCmd.h"
#include "MrDef.h"
#include "MrPro.h"
#include "MrUdp.h"
#include "MrPoll.h"
#include "MrTime.h"

int main(int argc, char* argv[])
{
	CMrPro CPro;
	CMrUdp CUdp;
	CMrTime CTime;
	int iLength = 0;
	USHORT usCount = 0;
	USHORT usTotal = 0;
	MRPROKV_S stKv[MR_CMD];
	UCHAR uszPacket[MR_PACKET] = { 0 };

	if (argc < 3) {
		MR_PRINT("Usage: %s id1 id2 ... idn\n", argv[0]);
		return MR_SUCCESS;
	}

	/* body */
	usTotal = argc - 1;
	memset(stKv, '\0', sizeof(MRPROKV_S) * MR_CMD);
	for (usCount = 0; usCount < usTotal; usCount ++) {
		stKv[usCount].m_ucKey = MR_PL_HOST;
		stKv[usCount].m_iLength = strlen(argv[usCount + 1]);
		stKv[usCount].m_puszValue = (UCHAR*)argv[usCount + 1];
	}
	iLength = MrProKvOutput(stKv, usTotal, &uszPacket[MR_PRO_HEAD], MR_PACKET - MR_PRO_HEAD);

	/* head */
	CPro.MrProPutBody(NULL);
	CPro.MrProPutReal(MR_PRO_0);
	CPro.MrProPutTotal(usTotal);
	CPro.MrProPutLength(iLength);
	CPro.MrProPutAttr(MR_PL_AVG);
	CPro.MrProPutExpand(MR_PRO_0);
	CPro.MrProPutVerb(MR_DEF_SELT);
	CPro.MrProPutType(MR_DEF_POLL);
	CPro.MrProPutFlag(MR_PRO_FLAG);
	CPro.MrProPutVersion(MR_PRO_1);
	CPro.MrProPutResponse(MR_PRO_Y);
	CPro.MrProPutResult(MR_DEF_SUCC);
	CPro.MrProPutProtocol(MR_PRO_CTRL);
	CPro.MrProCreate(uszPacket, MR_PACKET);

	CTime.MrTimeOn();

	if (MR_SUCCESS == CUdp.MrNetInitSend((char*)"172.16.8.211", (char*)"23011")) {
		CUdp.MrNetSetTimeOut(MR_TIMEOUT, 0);
		if (CUdp.MrNetSend(uszPacket, MR_PRO_HEAD + iLength) > 0) {
			memset(uszPacket, '\0', MR_PACKET);
			iLength = CUdp.MrNetRecv(uszPacket, MR_PACKET);
			if (iLength > 0) {
				/* head */
				CPro.MrProParse(uszPacket, iLength);
				CPro.MrProDisplay();
				/* body */
				if (CPro.MrProGetLength() > 0) {
					memset(stKv, '\0', sizeof(MRPROKV_S) * MR_CMD);
					MrProKvInput(stKv, CPro.MrProGetTotal(), CPro.MrProGetBody(), CPro.MrProGetLength());
					MrProKvDisplay(stKv, CPro.MrProGetTotal());
				}
				else {
					MR_PRINT("Failed to MrProGetLength\n");
				}
			}
			else {
				MR_ERROR("Failed to MrNetRecv\n");
			}
		}
		else {
			MR_ERROR("Failed to MrNetSend\n");
		}
		CUdp.MrNetClose();
	}

	CTime.MrTimeOff();
	CTime.MrTimeDisplay();

	return MR_SUCCESS;
}
